package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fowo.api.inventory.entity.Inventory;
import com.fowo.api.inventory.mapper.InventoryMapper;
import com.fowo.api.inventory.model.InventoryStorageVo;
import com.fowo.api.inventory.model.SimpleInventoryItem;
import com.fowo.api.model.inventory.list.subtable.InventoryListSubtableItemVo;
import com.fowo.api.model.ship.plans.ShipPlansReleaseInventoryAction;
import com.fowo.api.model.ship.plans.ShipPlansVo;
import com.fowo.api.model.ship.plans.sub.ShipPlansSubItemVo;
import com.fowo.api.service.ShipPlansService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Component
public class ShipPlansReleaseInventory implements ShipPlansReleaseInventoryAction {

    @Resource
    private ShipPlansService shipPlansService;
    @Resource
    private InventoryMapper inventoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(List<Long> idList) throws Exception {
        for(Long id : idList){
            ShipPlansVo shipPlans = shipPlansService.getVoById(id);
            releaseInventory(shipPlans);
        }
    }

    /**
     * 释放库存
     * @param shipPlans
     */
    public void releaseInventory(ShipPlansVo shipPlans) throws Exception {
        List<ShipPlansSubItemVo> productList = shipPlans.getProductList();
        for(ShipPlansSubItemVo subtable : productList){
            SimpleInventoryItem inventoryItem = new SimpleInventoryItem();
            inventoryItem.setId(subtable.getProductId());

            InventoryStorageVo inventoryStorageVo = new InventoryStorageVo();
            inventoryStorageVo.setStockId(subtable.getDeliveryWarehouseId());
            inventoryStorageVo.setItem(inventoryItem);
            inventoryStorageVo.setShopId(subtable.getShopId() == null ? null : subtable.getShopId().toString());

            List<Inventory> inventoryList =inventoryMapper.selectByStorageOne(inventoryStorageVo);
            Inventory inventory = null;
            if (CollectionUtils.isNotEmpty(inventoryList)){
                inventory=inventoryList.get(0);
            }else {
//                throw new Exception("当前仓库没有库存");
                throw new Exception("当前仓库没有产品 "+subtable.getProductIdName()+" 的库存");
            }

            LambdaUpdateChainWrapper<Inventory> inventoryUpdateChainWrapper = new LambdaUpdateChainWrapper<>(inventoryMapper);
            inventoryUpdateChainWrapper.eq(Inventory::getId, inventory.getId());
            inventoryUpdateChainWrapper.eq(Inventory::getVersion, inventory.getVersion());
            inventoryUpdateChainWrapper.set(Inventory::getVersion, inventory.getVersion() + 1);
            inventoryUpdateChainWrapper.set(Inventory::getLockQty, inventory.getLockQty().subtract(BigDecimal.valueOf(subtable.getPlannedShipment())));
            inventoryUpdateChainWrapper.set(Inventory::getStockQty, inventory.getStockQty().add(BigDecimal.valueOf(subtable.getPlannedShipment())));
            if(!inventoryUpdateChainWrapper.update()) {
                throw new Exception("数据版本错误，更新失败");
            }

            subtable.setIsLock(false);
        }
        shipPlansService.update(shipPlans);
    }
}
